package news.yp.newsproject.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;
import news.yp.newsproject.R;
import news.yp.newsproject.listener.OnItemClickListener;

public class MyRecyViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private List<String> lists ;
    private Context mContext;
    private List<String> datelists;
    private List<String> imglist;
    private List<String> titlelist;
    private OnItemClickListener itemClickListener;
    boolean hasmaore = false;
    private int normalType = 0;     // 第一种ViewType，正常的item
    private int footType = 1;       // 第二种ViewType，底部的提示View

    //上拉加载更多
    public static final int  PULLUP_LOAD_MORE=0;
    //正在加载中
    public static final int  LOADING_MORE=1;
    //没有更多数据了
    public static final int NO_MORE_DATA=2;

    //上拉加载更多状态-默认为0
    private int load_more_status=0;



    public MyRecyViewAdapter(Context context,List<String> datelists,List<String> imgist,List<String> titlelist){
        this.mContext = context;
       // this.lists = lists;
        this.datelists = datelists;
        this.imglist = imgist;
        this.titlelist = titlelist;
    }

    public void refrsh(List<String> newdatelists, List<String> newimglist, List<String> newtitlelist) {
        datelists.clear();
        imglist.clear();
        titlelist.clear();
        datelists.addAll(newdatelists);
        imglist.addAll(newimglist);
        titlelist.addAll(newtitlelist);
        hasmaore = true;
        notifyDataSetChanged();
    }
    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     * @param status
     */
    public void changeMoreStatus(int status){
        Log.e("TAG","开始刷新当前的数据数量是："+titlelist.size());
        load_more_status=status;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        if (viewType == normalType) {
         //   return new NormalHolder(LayoutInflater.from(context).inflate(R.layout.item, null));
            View view = LayoutInflater.from(mContext).inflate(R.layout.recy_item, viewGroup, false);
            NormalHolder holder = new NormalHolder(view,itemClickListener);
            return holder;
        } else {
            return new FootHolder(LayoutInflater.from(mContext).inflate(R.layout.footview_layout, viewGroup,false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof NormalHolder) {
              randmsetBag(((NormalHolder)viewHolder));
          //  ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_one);
            ((NormalHolder)viewHolder).mTitle.setText(titlelist.get(position));
            ((NormalHolder)viewHolder).mdate.setText(datelists.get(position));
            Glide.with(mContext).load(imglist.get(position))
                    .placeholder(R.mipmap.pic_loading)
                    .error(R.mipmap.pic_loaderro)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(((NormalHolder)viewHolder).mimgView);
        } else {
            switch (load_more_status){
                case PULLUP_LOAD_MORE:
                    Log.e("TAG","当前的状态是：上拉加载更多");
                   // ((FootHolder)viewHolder).tips.setVisibility(View.VISIBLE);
                    ((FootHolder)viewHolder).tips.setText("上拉加载有惊喜哟...");
                    ((FootHolder)viewHolder).tips.setTextColor(mContext.getResources().getColor(android.R.color.holo_blue_light));
                    break;
                case LOADING_MORE:
                    Log.e("TAG","当前的状态是：正在加载更多数据");
                    ((FootHolder)viewHolder).tips.setVisibility(View.VISIBLE);
                    ((FootHolder)viewHolder).tips.setText("殷同学正在赶来的路上...");
                    ((FootHolder)viewHolder).tips.setTextColor(mContext.getResources().getColor(android.R.color.holo_red_dark));
                    break;
                case NO_MORE_DATA:
                    Log.e("TAG","当前的状态是：正在加载更多数据");
                    ((FootHolder)viewHolder).tips.setVisibility(View.VISIBLE);
                    ((FootHolder)viewHolder).tips.setText("客官不骗您，本店没有数据了哟...");
                    ((FootHolder)viewHolder).tips.setTextColor(mContext.getResources().getColor(android.R.color.holo_orange_dark));

                    break;
            }
        }
//        if (position+1==getItemCount()){
//            ((FootHolder)viewHolder).tips.setVisibility(View.VISIBLE);
//        }
        }

    private void randmsetBag(NormalHolder viewHolder) {
        int a = (int)(Math.random()*6)+1;
        switch (a){
            case 1:
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_one);
                break;
            case 2:
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_two);
                break;
            case 3:
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_three);
                break;
            case 4:
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_four);
                break;
            case 5:
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_five);
                break;
            case 6:
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_six);
                break;
            default:
                Log.e("TAG","不可能吧");
                ((NormalHolder)viewHolder).mLinealayout.setBackgroundResource(R.mipmap.recy_item_bcg_one);
        }
    }

    @Override
    public int getItemViewType(int position) {

        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }

    @Override
    public int getItemCount() {
        return titlelist.size()+1;
    }



  class NormalHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView mTitle;
        private ImageView mimgView;
        private TextView mdate;
        private View mView;
        private OnItemClickListener listener;
        private LinearLayout mLinealayout;
        private ImageView imgshare;

        public NormalHolder(View itemview,OnItemClickListener itemClickListener){
            super(itemview);
            listener = itemClickListener;
            mTitle = (TextView)itemview.findViewById(R.id.recy_title);
            mimgView = (ImageView)itemview.findViewById(R.id.recy_itemimg);
            mdate = (TextView)itemview.findViewById(R.id.recy_date);
            mLinealayout = (LinearLayout)itemview.findViewById(R.id.recyle_line_bg);
            imgshare = (ImageView)itemview.findViewById(R.id.shoucang_img);
            mTitle.setOnClickListener(this);
            imgshare.setOnClickListener(this);
//            imgshare.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    imgshare.setSelected(!imgshare.isSelected());
//                }
//            });
        }
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.recy_title:
                    listener.onItemOnClick(view, getAdapterPosition());
                    break;
                case R.id.shoucang_img:
                   // imgshare.setSelected(!imgshare.isSelected());
                    Toast.makeText(mContext, "还想收藏，能看看就可以拉～～", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    // // 底部footView的ViewHolder，用以缓存findView操作
    class FootHolder extends RecyclerView.ViewHolder {
        private TextView tips;

        public FootHolder(View itemView) {
            super(itemView);
            tips = (TextView) itemView.findViewById(R.id.foot_tips);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.itemClickListener = onItemClickListener;
    }


}
